package com.gxa.learning.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.util.ReturnJsonResultUtil;
import com.gxa.learning.pc.service.ExamineApprovalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * 审批流程的controller
 *
 * @author lijianmin
 * @date 2021/12/02  18:45:49
 */
@RestController
@RequestMapping("/exap")
@Api(tags = "审批流程的接口")
public class ExamineApprovalController {

    @Autowired
    private ExamineApprovalService examineApprovalService;

    /**
     * 添加审批流程的接口
     *
     * @param map 审批流程的map
     * @return 返回是否成功
     */
    @PostMapping("add")
    @ApiOperation("添加审批流程的接口")
    @ApiImplicitParam(name = "map", value = "map", paramType = "body", dataType = "Map")
    public Map<String, Object> add(@RequestBody Map<String, Object> map) throws BusinessException {
        examineApprovalService.add(map);
        //给前端返回消息
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功!");
        return jsonResult.getDataMap();
    }

    /**
     * 修改审批流程的接口
     *
     * @param map 审批流程修改的信息
     * @return 返回是否成功
     */
    @PostMapping("update")
    @ApiOperation("修改审批流程的接口")
    @ApiImplicitParam(name = "map", value = "审批流程对象", dataType = "map", paramType = "body")
    public Map<String, Object> update(@RequestBody Map<String, Object> map) throws BusinessException {
        Boolean aBoolean = examineApprovalService.updateExamineApproval(map);
        return ReturnJsonResultUtil.ReturnJsonResultUtil(aBoolean, "修改");
    }

    /**
     * 删除审批流程的接口
     *
     * @param map 审批流程的id(前端需要使用body接收 所以更改为map)
     * @return 返回是否成功
     */
    @PostMapping("del")
    @ApiOperation("删除审批流程的接口")
    @ApiImplicitParam(name = "map", value = "编号", dataType = "Map", paramType = "body", required = true)
    public Map<String, Object> del(@RequestBody Map<String, Object> map) throws BusinessException {
        if (map.get("id") == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_NOT_NULL);
        }
        Boolean aBoolean = examineApprovalService.deleteById(Long.valueOf(map.get("id").toString()));
        return ReturnJsonResultUtil.ReturnJsonResultUtil(aBoolean, "删除");
    }

    /**
     * 根据条件搜索
     *
     * @param name             流程名称
     * @param state            流程状态
     * @param type             流程类型
     * @param departmentName   所属部门
     * @param creationTime     生效日期
     * @param uploadPeopleName 上传人
     * @param page             页码
     * @param size             每页数据量
     * @return 返回查询到的集合
     */
    @GetMapping("select")
    @ApiOperation("根据条件搜索(如果条件为空 则为全查询)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "流程名称", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "流程状态", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "流程类型", paramType = "query"),
            @ApiImplicitParam(name = "departmentName", value = "所属部门", paramType = "query"),
            @ApiImplicitParam(name = "creationTime", value = "生效日期", paramType = "query"),
            @ApiImplicitParam(name = "uploadPeopleName", value = "上传人", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query")
    })
    public Map<String, Object> select(@RequestParam(name = "name", required = false) String name
            , @RequestParam(name = "state", required = false) Integer state
            , @RequestParam(name = "type", required = false) String type
            , @RequestParam(name = "departmentName", required = false) String departmentName
            , @RequestParam(name = "creationTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date creationTime
            , @RequestParam(name = "uploadPeopleName", required = false) String uploadPeopleName
            , @RequestParam(name = "page", required = false) Integer page
            , @RequestParam(name = "size", required = false) Integer size) throws BusinessException {

        PageInfo<Map<String, Object>> mapPageInfo = examineApprovalService.selectByLikes(name, state, type, departmentName, creationTime, uploadPeopleName, page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功!");
        jsonResult.setData(mapPageInfo.getList());
        jsonResult.put("count", mapPageInfo.getTotal());
        return jsonResult.getDataMap();
    }


    /**
     * 通过审批流程的接口
     *
     * @param map 审批流程的编号
     * @return 返回消息
     * @throws BusinessException 操作中产生的异常
     */
    @PostMapping("/pass")
    @ApiOperation("通过审批流程的接口")
    @ApiImplicitParam(name = "map", value = "map", paramType = "body")
    public Map<String, Object> pass(@RequestBody Map<String, Object> map) throws BusinessException {
        if (map.get("id") == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_NOT_NULL);
        }
        examineApprovalService.pass(Long.valueOf(map.get("id").toString()));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功!");
        return jsonResult.getDataMap();
    }

    /**
     * 驳回审批流程的接口
     *
     * @param map 审批流程的编号
     * @return 返回消息
     * @throws BusinessException 操作中产生的异常
     */
    @PostMapping("/reject")
    @ApiOperation("驳回审批流程的接口")
    @ApiImplicitParam(name = "map", value = "map", paramType = "body")
    public Map<String, Object> reject(@RequestBody Map<String, Object> map) throws BusinessException {
        if (map.get("id") == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_NOT_NULL);
        }
        examineApprovalService.reject(Long.valueOf(map.get("id").toString()));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功!");
        return jsonResult.getDataMap();
    }

}
